package com.qc.sys.web;

import com.google.common.collect.Lists;
import com.qc.common.utils.StringUtils;
import com.qc.common.web.BaseController;
import com.qc.sys.entity.Role;
import com.qc.sys.entity.User;
import com.qc.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户Controller
 */
@Controller
@RequestMapping(value = "sys/user")
public class UserController extends BaseController {

	@Autowired
	private SystemService systemService;
	

	@ModelAttribute("user")
	public User get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return systemService.getUser(id);
		}else{
			return new User();
		}
	}
	
	@RequiresPermissions("sys:user:view")
	@RequestMapping(value = {"list", ""})
	public String list(User user, Model model) {
		List<User> list = systemService.findUserByPage(user);
		model.addAttribute("list",list);
		setTitle(model, "用户列表");
		return "sys.user.userList";
	}

	@RequiresPermissions("sys:user:view")
	@RequestMapping(value = "form")
	public String form(User user, Model model) {
		model.addAttribute("user", user);
		List<Role> list = systemService.findAllRole();
		model.addAttribute("roleList", list);
		if (StringUtils.isNotBlank(user.getId())) {
			setTitle(model, "用户编辑");
		} else {
			setTitle(model, "用户添加");
		}
		return "sys.user.userForm";
	}
	
	@RequiresPermissions("sys:user:edit")
	@RequestMapping(value = "save")
	public String save(User user, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		// 如果新密码为空，则不更换密码
		if (StringUtils.isNotBlank(user.getNewPassword())) {
			user.setPassword(SystemService.entryptPassword(user.getNewPassword()));
		}
		if (!beanValidator(model, user)){
			return form(user, model);
		}
		// 角色数据有效性验证，过滤不在授权内的角色
		List<Role> roleList = Lists.newArrayList();
		String roleIds = user.getRoleIds();
		for (Role r : systemService.findAllRole()){
			if (roleIds.contains(r.getId())){
				roleList.add(r);
			}
		}
		user.setRoleList(roleList);
		systemService.saveUser(user);
		addMessage(redirectAttributes, "保存用户 [ " + user.getNickname() + " ] 成功");
		redirectAttributes.addFlashAttribute("redirect", request.getContextPath()+"/sys/user");
		return "redirect:/sys/user";
	}

	@RequiresPermissions("sys:user:edit")
	@RequestMapping(value = "saveResp")
	public String saveResp(User user, Model model,HttpServletResponse response) {
		if (!beanValidator(model, user)){
			return form(user, model);
		}
		systemService.saveUser(user);
		return renderString(response,"保存 [ " + user.getNickname() + " ] 成功", "text/html;charset=UTF-8");
	}
	
	@RequiresPermissions("sys:user:edit")
	@RequestMapping(value = "delete")

	public String delete(User user, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		systemService.deleteUser(user);
		addMessage(redirectAttributes, "删除用户成功");
		redirectAttributes.addFlashAttribute("redirect", request.getContextPath()+"/sys/user");
		return "redirect:/sys/user";
	}

}
